package spider;

import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import jxl.write.WriteException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class CsmbriUtil1{
	//534473
	//451拉取失败
	//1049
	//1750
	public static void main(String[] args) {
		doIt();
	}
	
	public static void doIt(){
//		getFile(451);
//		getFile(1001);
		getFile(1701);
		
		/*getFile(1);
		getFile(51);
		getFile(101);
		getFile(151);
		getFile(201);
		getFile(251);
		getFile(301);
		getFile(351);
		getFile(401);
		getFile(451);
		
		getFile(501);
		getFile(551);
		getFile(601);
		getFile(651);
		getFile(701);
		getFile(751);
		getFile(801);
		getFile(851);
		getFile(901);
		getFile(951);
		
		getFile(1001);
		getFile(1051);
		getFile(1101);
		getFile(1151);
		getFile(1201);
		getFile(1251);
		getFile(1301);
		getFile(1351);
		getFile(1401);
		getFile(1451);
		
		getFile(1501);
		getFile(1551);
		getFile(1601);
		getFile(1651);
		getFile(1701);
		getFile(1751);*/
	}
	
	
	//http://www.csmbri.com/data/ifo/t2.htm
	
	public static void getFile(int pageIndex) {
		
		String fileName =pageIndex+".xlsx";
		Long s = null;
		Long e = null;
		int maxPage = pageIndex + 50;
			for(int page=pageIndex;page<maxPage;page++){
				try {
					s = new Date().getTime();
					System.out.println("正在拉取第："+page+"页数据，还剩："+(maxPage-page-1)+"页数据");
					JSONObject pageData = getData(page);
					writeData(pageData,"c://file1//"+fileName);
//					writeData(pageData,File.separator+"root"+File.separator+"file1"+File.separator+fileName);
					e = new Date().getTime();
					System.out.println("写入第一页成功,耗时："+(e-s)/1000+"秒");
				} catch (Exception ee) {
					ee.printStackTrace();
					System.out.println("写入第页"+page+"失败："+ee.getMessage());
				}
			}
	}
	
	public static JSONObject getData(int page){
		String url = "http://www.csmbri.com/data/ifo/t1/json?_search=false&nd=1481595271952&rows=300&page="+page+"&sord=asc";
		JSONObject  ret = CommonUtil.httpRequest(url, "POST", "");
//		System.out.println(ret.toString());
		
		
		
		return ret;
	}
	
	public static void writeData(JSONObject ret,String fileName) throws WriteException, FileNotFoundException, Exception{
		
		
		String[] title = new String[]{
				"填报年月", "账号", "名称",
				"市", "区（县、市）", "乡镇 （街道）", "其他说明", "企业负责人", "填表负责人", "填表人", "联系电话", "手机", "填报日期",
				"应收账款-本月止累计", "应收账款-去年本月止累计", "应收账款-累计同比增长",
				"存货-本月止累计", "存货-去年本月止累计","存货-累计同比增长",
				"产成品-本月止累计", "产成品-去年本月止累计", "产成品-累计同比增长", 
				"资产总计-本月止累计","资产总计-去年本月止累计", "资产总计-累计同比增长", 
				"货债合计-本月止累计", "货债合计-去年本月止累计", "货债合计-累计同比增长",
				
				"营业收入-本月止累计", "营业收入-去年本月止累计", "营业收入-累计同比增长",
				"主营业收入-本月止累计", "主营业收入-去年本月止累计","主营业收入-累计同比增长","主营业收入-本月","主营业收入-去年本月",
				"营业成本-本月止累计", "营业成本-去年本月止累计","营业成本-累计同比增长",
				"主营业务成本-本月止累计", "主营业务成本-去年本月止累计", "主营业务成本-累计同比增长",
				
				"营业税金及附加-本月止累计","营业税金及附加-去年本月止累计", "营业税金及附加-累计同比增长",
				"销售费用-本月止累计", "销售费用-去年本月止累计", "销售费用-累计同比增长",
				"管理费用-本月止累计", "管理费用-去年本月止累计", "管理费用-累计同比增长",
				"其中：税金-本月止累计", "其中：税金-去年本月止累计","其中：税金-累计同比增长",
				"财务费用-本月止累计", "财务费用-去年本月止累计", "财务费用-累计同比增长",
				"其中：利息支出-本月止累计","其中：利息支出-去年本月止累计", "其中：利息支出-累计同比增长",
				"投资收益-本月止累计", "投资收益-去年本月止累计", "投资收益-累计同比增长",
				"利润总额-本月止累计", "利润总额-去年本月止累计", "利润总额-累计同比增长",
				"应交所得税-本月止累计", "应交所得税-去年本月止累计","应交所得税-累计同比增长",
				"应交增值税-本月止累计", "应交增值税-去年本月止累计", "应交增值税-累计同比增长",
				"应付职工薪酬-本月止累计","应付职工薪酬-去年本月止累计", "应付职工薪酬-累计同比增长",
				"从业人员平均人数-本月止累计", "从业人员平均人数-去年本月止累计", "从业人员平均人数-累计同比增长",
				"银行贷款余额（本年贷方累计发生额）-本月止累计", "银行贷款余额（本年贷方累计发生额）-去年本月止累计", "银行贷款余额（本年贷方累计发生额）-累计同比增长", 
				"（一）科技活动经费支出总额-本月止累计", "（一）科技活动经费支出总额-去年本月止累计","（一）科技活动经费支出总额-累计同比增长", 
				"固定资产投资额-本月止累计", "固定资产投资额去年本月止累计", "固定资产投资额累计同比增长", 
				"生产能力（设备）利用率-本月止累计","生产能力（设备）利用率-去年本月止累计", "生产能力（设备）利用率-累计同比增长",
				"纳税总额-本月止累计", "纳税总额-去年本月止累计", "纳税总额-累计比去年同期增长（%）", "纳税总额-本月", "纳税总额-去年本月",
				"本单位行业代码", 
				"本单位行业代码(全)", 
				"本单位行业代码名称"
				};
		
		
		if("0".equals(ret.getString("code")))
		{
			JSONArray rows = ret.getJSONArray("rows");
			List<Object[]> list = new ArrayList<Object[]>();
			for(int i=0;i<rows.size();i++)
			{
				JSONObject row = rows.getJSONObject(i);
				Object[] data = new Object[98];
				data[0] = row.getString("bdate").substring(0, 10);
				data[1] = trans(row.getString("f001"));//*
				data[2] = row.getString("f002");
				data[3] = row.getString("f003");//市
				
				data[4] = row.getString("f004");//*
				data[5] = row.getString("f005");//*
				data[6] = row.getString("f006");//*
				data[7] = row.getString("f007");//*
				data[8] = row.getString("f008");//*填表负责人
				data[9] = row.getString("f009");//*
				data[10] = trans(row.getString("f010"));//*联系电话
				data[11] = trans(row.getString("f011"));//*手机
				data[12] = row.getString("f013");//*
				
				data[13] = row.getString("f012");//1
				data[14] = row.getString("f014");
				data[15] = row.getString("f015");
				data[16] = row.getString("f016");
				data[17] = row.getString("f017");
				data[18] = row.getString("f018");
				data[19] = row.getString("f019");
				data[20] = row.getString("f020");
				
				
				data[21] = row.getString("f021");//2
				data[22] = row.getString("f022");
				data[23] = row.getString("f023");
				data[24] = row.getString("f024");
				data[25] = row.getString("f025");
				data[26] = row.getString("f026");
				data[27] = row.getString("f027");
				data[28] = row.getString("f028");
				data[29] = row.getString("f029");
				data[30] = row.getString("f030");
				
				data[31] = row.getString("f031");//3
				data[32] = row.getString("f032");
				data[33] = row.getString("f033");
				data[34] = row.getString("f034");
				data[35] = row.getString("f035");
				data[36] = row.getString("f036");
				data[37] = row.getString("f037");
				data[38] = row.getString("f038");
				data[39] = row.getString("f039");
				data[40] = row.getString("f040");
				
				data[41] = row.getString("f041");//4
				data[42] = row.getString("f042");
				data[43] = row.getString("f043");
				data[44] = row.getString("f044");
				data[45] = row.getString("f045");
				data[46] = row.getString("f046");
				data[47] = row.getString("f047");
				data[48] = row.getString("f048");
				data[49] = row.getString("f049");
				data[50] = row.getString("f050");
				
				data[51] = row.getString("f051");//5
				data[52] = row.getString("f052");
				data[53] = row.getString("f053");
				data[54] = row.getString("f054");
				data[55] = row.getString("f055");
				data[56] = row.getString("f056");
				data[57] = row.getString("f057");
				data[58] = row.getString("f058");
				data[59] = row.getString("f059");
				data[60] = row.getString("f060");
				
				data[61] = row.getString("f061");//6
				data[62] = row.getString("f062");
				data[63] = row.getString("f063");
				data[64] = row.getString("f064");
				data[65] = row.getString("f065");
				data[66] = row.getString("f066");
				data[67] = row.getString("f067");
				data[68] = row.getString("f068");
				data[69] = row.getString("f069");
				data[70] = row.getString("f070");
				
				data[71] = row.getString("f071");//7
				data[72] = row.getString("f072");
				data[73] = row.getString("f073");
				data[74] = row.getString("f074");
				data[75] = row.getString("f075");
				data[76] = row.getString("f076");
				data[77] = row.getString("f077");
				data[78] = row.getString("f078");
				data[79] = row.getString("f079");
				data[80] = row.getString("f080");
				
				data[81] = row.getString("f081");//8
				data[82] = row.getString("f082");
				data[83] = row.getString("f083");
				data[84] = row.getString("f084");
				data[85] = row.getString("f085");
				data[86] = row.getString("f086");
				data[87] = row.getString("f087");
				data[88] = row.getString("f088");
				data[89] = row.getString("f089");
				data[90] = row.getString("f090");
				
				data[91] = row.getString("f091");//9
				data[92] = row.getString("f092");
				data[93] = row.getString("f093");
				data[94] = row.getString("f094");
				data[95] = row.getString("f095");
				data[96] = row.getString("f096");
				data[97] = row.getString("f097");
				
				
				
				list.add(data);
			}
			File f= new File(fileName);
			//写入Excel
			ExcelUtil.createExcel(f, 
					list, title, "工业产销监测数据", new String[]{
							"30","30","30","30","30","30","30","30","30","30",
							"30","30","30","30","30","30","30","30","30","30",
							"30","30","30","30","30","30","30","30","30","30",
							"30","30","30","30","30","30","30","30","30","30",
							"30","30","30","30","30","30","30","30","30","30",
							"30","30","30","30","30","30","30","30","30","30",
							"30","30","30","30","30","30","30","30","30","30",
							"30","30","30","30","30","30","30","30","30","30",
							"30","30","30","30","30","30","30","30","30","30",
							"30","30","30","30","30","30","30","30"
							},f.exists());
		}else
		{
			System.out.println("拉取数据异常");
		}
		
		
		
	}
	
	public static String trans(String str){
		try {
			BigDecimal db = new BigDecimal(str);
			return db.toString();
		} catch (Exception e) {
			return str;
		}
	}

}
